package test_achieve.courierapp;

import constant.Constant;
import cucumber.api.PendingException;
import cucumber.api.java.en.Given;
import org.junit.Assert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import test_achieve.Hooks;
import util.tools.BusinessTools;
import util.tools.CommonTools;

import java.util.List;

/**
 * Created by chenkuo on 2017/12/16.
 */
public class DistributeOrder {

    @Given("^会弹出派单页面：(.*)$")
    public void 会弹出派单页面(String user) throws Throwable {
        try {
            CommonTools.waitElementLoad(Constant.driverCourier,"id","grab_task");
            //等待抢单页面出现
            System.out.println("抢单页面出现，不会走接口抢单");
            System.out.println("hahahahahahaaha");
        }catch (Exception e){
            BusinessTools.pushExcludeNetworkImpact(user);
        }
        if (Constant.interfaceGrabStatus){
            System.out.println("接口抢单，不需要校验派单页面");
        }else {
            String icon = Constant.driverCourier.findElement(By.name("派")).getText();
            Assert.assertEquals(icon,"派");
            CommonTools.checkElementExit(Constant.driverCourier,"id","grab_task");
        }

    }

    @Given("^会弹出派单页面$")
    public void 会弹出派单页面() throws Throwable {
        CommonTools.waitElementLoad(Constant.driverCourier,"id","grab_task");
    }

    @Given("^不会弹出派单页面$")
    public void 不会弹出派单页面() throws Throwable {
        Thread.sleep(6000);
        boolean btn = CommonTools.checkElementExit(Constant.driverCourier,"id", "grab_task");
        Assert.assertEquals(false, btn);
    }

    @Given("^点击接受按钮$")
    public void 点击接受按钮() throws Throwable {
        if (Constant.interfaceGrabStatus){
            System.out.println("接口抢单，不需要点击接受派单");
        }else {
            Constant.driverCourier.findElement(By.id("grab_task")).click();//点击接受派单按钮
             }
    }


    @Given("^关闭派单接受页面$")
    public void 关闭派单接受页面() throws Throwable {
        Constant.driverCourier.findElement(By.id("close")).click();//派单页面，点击右上“X”
    }

    @Given("^p客服流单$")
    public void p客服流单() throws Throwable {
        Hooks hooks = new Hooks();
        hooks.cancelOrder();
    }

    @Given("^确认订单取消$")
    public void 确认订单取消() throws Throwable {
        try {
            Constant.driverCourier.findElement(By.id("btnOk")).click();//点击‘我知道了’
        }catch (Exception e){
            System.out.println("没有弹出取消提醒");
        }
    }

    @Given("^接受派单后客服接口流单$")
    public void 接受派单后客服接口流单() throws Throwable {
        if (Constant.interfaceGrabStatus){
            System.out.println("接口抢单，不需要点击抢单按钮");
        }else {
            点击接受按钮();
        }
        p客服流单();
        确认订单取消();
    }

    @Given("^等待50秒$")
    public void 等待50秒 ()throws Throwable {
        Thread.sleep(40000);
    }

    @Given("^会弹出拒单提醒$")
    public void 会弹出拒单提醒() throws Throwable {
        if (Constant.interfaceGrabStatus){
            throw new Exception("接口完成订单，不会弹出拒单提醒");
        }
        CommonTools.waitElementLoad(Constant.driverCourier,"id","tvTitle");
    }

    @Given("^不会弹出拒单提醒$")
    public void 不会弹出拒单提醒() throws Throwable {
        if (Constant.interfaceGrabStatus){
            throw new Exception("接口抢单，不会弹出拒单提醒");
        } else {
            boolean btn = CommonTools.checkElementExit(Constant.driverCourier,"id","tvTitle");
            Assert.assertEquals(btn,false);
        }
    }

    @Given("^关闭拒单提醒页面$")
    public void 关闭拒单提醒页面() throws Throwable {
        Constant.driverCourier.findElement(By.id("btnOk")).click();//点击：我知道了
    }

    @Given("^数据库中检查拒单类型为超时拒单$")
    public void 数据库中检查拒单类型为超时拒单() throws Throwable {

    }

    @Given("^关闭派单页面$")
    public void 关闭派单页面() throws Throwable {
        if (Constant.interfaceGrabStatus){
            throw new Exception("接口抢单，不会弹出拒单页面");
        }else {
            Constant.driverCourier.findElement(By.id("close")).click();//点击“X”关闭
        }
    }

    @Given("^确认手动拒单$")
    public void 确认手动拒单() throws Throwable {
        Constant.driverCourier.findElement(By.id("cancelbut")).click();//点击确认
    }

    @Given("^取消手动拒单$")
    public void 取消手动拒单() throws Throwable {
        Constant.driverCourier.findElement(By.id("okbut")).click();//点击取消
    }

    @Given("^获取拒单次数$")
    public void 获取拒单次数() throws Throwable {
        Constant.courierRefuseNumber = getRefuseNumber();
        System.out.println("当前拒单次数："+Constant.courierRefuseNumber);
    }
private int getRefuseNumber(){
        if (CommonTools.checkElementExit(Constant.driverCourier,"id","assignOrderNotice")){
        String notice = Constant.driverCourier.findElement(By.id("assignOrderNotice")).getText();
        return Integer.parseInt(CommonTools.splitNumber(notice,"已拒绝",1));
    }
    return 0;
}
    @Given("^确定拒单次数加一$")
    public void 确定拒单拒单次数加一() throws Throwable {
    Assert.assertEquals(Constant.courierRefuseNumber+1,getRefuseNumber());
    }

    @Given("^确定拒单次数不变$")
    public void 确定拒单次数不变() throws Throwable {
        Assert.assertEquals(Constant.courierRefuseNumber,getRefuseNumber());
    }
    private int getPendingOrderNumber(){
        if (CommonTools.checkElementExit(Constant.driverCourier,"id","tv_new_order_message")){
            String num = Constant.driverCourier.findElement(By.id("tv_new_order_message")).getText();
            return Integer.parseInt(num);
        }
        return 0;
    }
    @Given("^获取待抢单未读数$")
    public void 获取待抢单未读数() throws Throwable {
        Constant.courierPendingOrderNumber = getPendingOrderNumber();
    }

    @Given("^待抢单未读数加一$")
    public void 待抢单未读数加一() throws Throwable {
        int num = getPendingOrderNumber();
        Assert.assertEquals(Constant.courierPendingOrderNumber+1,num);
    }

    @Given("^没有未读的待抢单$")
    public void 没有未读的待抢单() throws Throwable {
        int num = getPendingOrderNumber();
        Assert.assertEquals(0,num);
    }

    @Given("^待抢单中检查是否有刚下订单$")
    public void 待抢单中检查是否有刚下订单() throws Throwable {
        List<WebElement> orderNumbers = Constant.driverCourier.findElements(By.id("tasknum_txt"));
        String orderNumber = orderNumbers.get(0).getText();
        Assert.assertEquals(Constant.OrderNumbuter.get(0),orderNumber);
    }
}
